// index.js
// 获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentList:[],
    total:"",
    imgUrl:'../image/zan.png',
    imgUrl2:'../image/sc.png',
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    console.log('文章id',options.article_id)
    //获取文章详情
    wx.request({
      url: 'https://www.tianba.work/api/article/detail?article_id='+options.article_id,
      success(res){
        console.log('文章详情',res)
        let time=[];
        time.push(util.formartDate(new Date(res.data.data.article.add_time*1000)));
       
        that.setData({
          article_title:res.data.data.article.article_title,
          thistime:time,
          content:res.data.data.article.content,
          likeNum:res.data.data.article.article_likeNum,
          collectNum:res.data.data.article.article_collectNum,
          imageUrl:res.data.data.article.cover_image,
          shareNum:res.data.data.article.article_shareNum

        })
       
        console.log(that.data.imageUrl)
     
      }
    })
    //获取点赞//获取收藏
    if(wx.getStorageSync('access_token')){
      wx.request({
        url: 'https://www.tianba.work/api/Userarticle/list',
        data:{
          session3rd:wx.getStorageSync('access_token'),
          article_likeId:that.options.article_id,
        },
        success:function(res){
          console.log(2222,res.data)
            if(res.data.status == 1001){
                  that.setData({
                    imgUrl:'../image/zan1.png'
                  })
            }
        }
      })  
      wx.request({
        url: 'https://www.tianba.work/api/Usershoucang/list',
        data:{
          session3rd:wx.getStorageSync('access_token'),
          article_collectId:that.options.article_id,
        },
        success:function(res){
          console.log(2222,res.data)
            if(res.data.status == 1001){
                  that.setData({
                    imgUrl2:'../image/sc1.png'
                  })
            }
        }
      })
    }
  

    //获取文章评论
    wx.request({
      
      url: 'https://www.tianba.work/api/comment/list?article_id='+options.article_id,
      success(res){
        console.log('文章评论',res)
        let time=[];
        console.log(res.data.data.commentList.length)
        for(let i=0;i<res.data.data.commentList.length;i++){
          time.push(util.formatTime(new Date(res.data.data.commentList[i].add_time*1000)));
        
        }
        that.setData({
          commentList:res.data.data.commentList,
          time:time,
          num:res.data.data.commentList.length
        })
        console.log("显示评论",that.data.commentList)
        
      }
     
    })
   
  },

  addComment:function(event){
    let that =this
    let articleId=that.options.article_id
    console.log(2222,that.options.article_id)
    let comment=event.detail.value
    console.log(event.detail.value)
    if(wx.getStorageSync('access_token')){
      if(comment==""){
        wx.showModal({
          title: '提示',
          content: '请先输入你要评论的内容',
          confirmText: '好的',
          confirmColor: '#ACB4E3',
          showCancel: false,
        });
      }
      else{
        wx.request({
          url: 'https://www.tianba.work/api/comment/add?article_id='+articleId,
          data:{
            comment_content:comment,
            nickName:wx.getStorageSync('userinfo').nickName,
            avatarUrl:wx.getStorageSync('userinfo').avatarUrl,
            session3rd:wx.getStorageSync('access_token'),
          },
          success(res){        
          if(res.data.status==10003){
            wx.showToast({
              title: '评论存在敏感词',
              icon: 'error',
              duration: 1500
            })
            that.setData({
              thisComment:"",         
            })
          }
          else{
            wx.showToast({
              title: '评论成功',
              icon: 'success',
              duration: 1500
            })
            console.log('评论',res)
            that.data.commentList.unshift(res.data.data.comment)
            console.log(2222,that.data.commentList)
            that.setData({
              commentList: that.data.commentList,
              thisComment:"",
              num:that.data.commentList.length
            })       
          }
          }
        })
      }
     
    }
    else{
      wx.switchTab({
        url: '/pages/person/person',
      })
      wx.showModal({
        title: '提示',
        content: '请先登录',
        confirmText: '好的',
        confirmColor: '#ACB4E3',
        showCancel: false,
      });
    }
    
 },

//点赞

Dianzan:function(){
  let that = this
  console.log(that.options.article_id)
  if(wx.getStorageSync('access_token')){
    if(that.data.imgUrl=="../image/zan.png"){
      wx.request({
        url: 'https://www.tianba.work/api/Userarticle/add',
        data:{
          session3rd:wx.getStorageSync('access_token'),
          article_likeId:that.options.article_id,
          nickName:wx.getStorageSync('userinfo').nickName,
          avatarUrl:wx.getStorageSync('userinfo').avatarUrl,
        },
        success:function(res){
          console.log(888888,res)
              that.setData({
                 imgUrl:"../image/zan1.png",
                 article_likeId:that.data.article_likeId,
                 likeNum:res.data.data.likeNum
              })
              wx.showToast({
                title: '点赞成功',
                icon: 'success',
                duration: 1500
              })
        }
      })
    }else{
      wx.request({
        url: 'https://www.tianba.work/api/Userarticle/edit',
        data:{
          session3rd:wx.getStorageSync('access_token'),
          article_likeId:that.options.article_id,
        },
        success:function(res){
          console.log(5666,res)
          that.setData({
             imgUrl:"../image/zan.png",
             likeNum:res.data.data.likeNum
          })
    }
      })
    }
  }else{
    wx.switchTab({
      url: '/pages/person/person',
    })
      wx.showModal({
        title: '提示',
        content: '请先登录',
        confirmText: '好的',
        confirmColor: '#ACB4E3',
        showCancel: false,
      });
    }
  
},


//收藏
onAddToFavorites(res){
  let that = this
  console.log(that.options.article_id)
  if(wx.getStorageSync('access_token')){
    console.log(111);
    if(that.data.imgUrl2=="../image/sc.png"){
      wx.request({
        url: 'https://www.tianba.work/api/Usershoucang/add',
        data:{
          session3rd:wx.getStorageSync('access_token'),
          article_collectId:that.options.article_id,
          nickName:wx.getStorageSync('userinfo').nickName,
          avatarUrl:wx.getStorageSync('userinfo').avatarUrl,
        },
        success:function(res){
          console.log(888888,res)
              that.setData({
                 imgUrl2:"../image/sc1.png",
                 article_collectId:that.data.article_collectId,
                 collectNum:res.data.data.collectNum
              })
        }
      })
    }
  }
  
},
  /**
   * 用户点击右上角分享
   */
onShareAppMessage:function() {
  console.log(1111);
  const promise = new Promise(resolve => {
    setTimeout(() => {
      resolve({
        title: this.data.article_title,
        imageUrl:this.data.imageUrl
      })
    }, )
  })
  let that = this
  if(wx.getStorageSync('access_token')){
      console.log(22222)
      wx.request({
        url: 'https://www.tianba.work/api/Usershare/add',
        data:{
          session3rd:wx.getStorageSync('access_token'),
          article_shareId:that.options.article_id,
          nickName:wx.getStorageSync('userinfo').nickName,
          avatarUrl:wx.getStorageSync('userinfo').avatarUrl,
        },
        success:function(res){
          console.log(7777777777,res)
              that.setData({
                article_shareId:that.data.article_shareId,
                shareNum:res.data.data.shareNum
              })
               wx.showToast({
                title: '分享成功',
                icon: 'success',
                duration: 1500
              })
        }
      })
  }
  return {
    title: this.data.article_title,
    path: '/pages/details/details?article_id='+this.options.article_id,
    imageUrl:this.data.imageUrl,
    promise 
  }
  

},
onShareTimeline: function(){
  const { info, code } = that.data;
  return {
    title: info.name,
    query: `code=${code}&uid=${wx.getStorageSync('uid')}`,
    imageUrl: info.thumbnail_url
  }
},



  /**
   * 生命周期函数--监听页面初次渲染完成
   */


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
 
  onPullDownRefresh: function () {
    let that =this
    that.onLoad(that.options)
    wx.stopPullDownRefresh() //刷新完成后停止下拉刷新动效
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
})